<template>
  <div class="app-container">
    <el-steps
      :active="2"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="步骤1" description="编辑基本信息"></el-step>
      <el-step title="步骤2" description="上传详细信息"></el-step>
      <el-step title="步骤3" description="信息审核确认"></el-step>
    </el-steps>
    <el-form ref="teacher" :model="teacher" label-width="80px" :label-position="labelPosition">
      <el-form-item label="教师头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="'http://120.27.131.93:8080/eduoss/fileoss/upload'"
          class="avatar-uploader"
        >
          <img :src="teacher.headshotUrl" style="width: 100px; height: 100px" />
        </el-upload>
      </el-form-item>
      <el-form-item label="教师工号">
        <el-input
          v-model="teacher.teacherId"
          style="width: 250px"
          :disabled="true"
        ></el-input>
      </el-form-item>

      <el-form-item label="密码">
        <el-input placeholder="请输入密码" v-model="teacher.password" style="width: 250px" show-password></el-input>
      </el-form-item>
      <el-form-item label="教师昵称">
        <el-input v-model="teacher.nickName" style="width: 250px"></el-input>
      </el-form-item>

      <el-form-item label="教师职称">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-select v-model="teacher.rank" placeholder="请选择职称">
              <el-option
                v-for="item in rankList"
                :key="item.dictName"
                :label="item.dictName"
                :value="item.dictName"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="学院">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-select v-model="teacher.collegeId" placeholder="请选择学院">
              <el-option
                v-for="item in CollegeList"
                :key="item.collegeId"
                :label="item.collegeName"
                :value="item.collegeId"
              />
            </el-select>
          </el-form-item>
        </el-form>
       
      </el-form-item>
    </el-form>

    <div class="btn">
        <el-button type="primary" @click="saveTeacher()">立即创建</el-button>
        <el-button>取消</el-button>
    </div>
  </div>
</template>

<script>
import dictApi from "@/api/dict";
import collegeApi from "@/api/college";
import Tinymce from "@/components/Tinymce"; //引入组件
import teacherApi from "@/api/teacher";
export default {
  components: { Tinymce },
  data() {
    return {
      labelPosition: "left",
      teacher: {},
      rankList: [],
      CollegeList: [],
    };
  },
  methods: {
    getTeacherById() {
      teacherApi.getTeacherById(this.teacher.teacherId).then((res) => {
        this.teacher = res.data.teacher;
      
        this.teacher.headshotUrl =
          "https://zhanghao-edu.oss-cn-beijing.aliyuncs.com/2021/09/22/c8aaed56-3d9d-4c67-8ead-36089c32f735zh.jpg";
      });
    },
    saveTeacher() {
      teacherApi.updateTeacher(this.teacher).then((res) => {
        //提示
        this.$message({
          type: "success",
          message: "编辑详细信息成功信息成功!",
        });
        //跳转到第二步
        this.$router.push({
          path: "/teacher/view/" + this.teacher.teacherId,
        });
      });
    },
    getDictList() {
      dictApi.getRankList().then((res) => {
        this.rankList = res.data.data;
      });
    },
    getCollege() {
      collegeApi.getCollegeList().then((res) => {
        this.CollegeList = res.data.page.list;
      });
    },
    //上传封面成功调用的方法
    handleAvatarSuccess(res, file) {
      this.teacher.headshotUrl = res.data.url;
    },
    //上传之前调用的方法
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    //获取路由id值
    if (this.$route.params && this.$route.params.id) {
      this.teacher.teacherId = this.$route.params.id;
      console.log(this.$route.params.id);
    }
    this.getDictList();
    this.getCollege();
    this.getTeacherById();
  },
};
</script>

<style>
.btn{
    margin: 10px 10px;
}
</style>